package com.ff.xyh.service.userfront.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.userfront.UserBlacker;
import com.ff.xyh.service.userfront.service.UserBlackerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户拉黑表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-25
 */
@RestController
@RequestMapping("/front-user")
public class UserBlackerController {

    @Autowired
    private UserBlackerService userBlackerService;

    @RequestMapping("insert-user-black")
    public ResultObject insertUserBlack(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "blackerId") String blackerId ) {
        // 未拉黑过
        if ( userBlackerService.getOne(
                new QueryWrapper<UserBlacker>()
                        .eq("user_id", id)
                        .eq("blacker_id", blackerId)) == null){
            if (userBlackerService.save(
                    new UserBlacker()
                            .setUserId(id)
                            .setBlackerId(blackerId))) {
                return ResultObject.ok().message("成功拉黑");
            } else {
                return ResultObject.error().message("拉黑失败");
            }
        } else {
            return ResultObject.error().message("拉黑失败, 请勿重复拉黑");
        }
    }

    @RequestMapping("get-user-black")
    public ResultObject getUserBlack(
            @RequestParam(name = "userId") String userId,
            @RequestParam(name = "blackerId") String blackerId ) {
        UserBlacker userBlacker = userBlackerService.getOne(
                new QueryWrapper<UserBlacker>()
                        .eq("user_id", userId)
                        .eq("blacker_id", blackerId));
        if ( userBlacker == null){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok().data("data", userBlacker);
        }
    }

    // 用户拉黑取消
    @RequestMapping("delete-user-black")
    public ResultObject deleteUserBlack(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "blackerId") String blackerId ) {
        if (userBlackerService.remove(
                new QueryWrapper<UserBlacker>()
                .eq("user_id", id)
                .eq("blacker_id", blackerId))) {
            return ResultObject.ok().message("成功取消拉黑");
        } else {
            return ResultObject.error().message("取消拉黑失败");
        }
    }

    /**
     * 删除用户所有拉黑信息
     * 删除用户或用户注销时使用
     * @param id
     * @return
     */
    @RequestMapping("delete-user-black-id")
    public ResultObject deleteUserBlackById(
            @RequestParam(name = "id") String id) {
        if (userBlackerService.remove(
                new QueryWrapper<UserBlacker>()
                .eq("user_id", id))) {
            return ResultObject.ok().message("成功删除该用户所有拉黑内容");
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_DELETE);
        }
    }
}

